package com.m3958.firstgwt.server.service;

import java.io.IOException;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;

import net.sf.json.JSONArray;

import clojure.lang.RT;
import clojure.lang.Var;

import com.google.inject.Inject;
import com.google.inject.Singleton;
import com.m3958.firstgwt.server.model.TableSchema;

@Singleton
public class ClojureService {
	
	private boolean cloaded = false;
	
	private boolean needReload = false;
	
	@Inject
	public ClojureService(PropertyReader pr,JedisPoolService jps) throws IOException{
		if(!cloaded){
			RT.loadResourceScript("com/m3958/firstgwt/server/servlet/ClojureServlet.clj");
			cloaded = true;
		}
		String os = System.getProperty("os.name").toLowerCase();
		if(os.indexOf("win") != -1){
			needReload = true;
		}
		
		Var initRedis = RT.var("com.m3958.firstgwt.server.servlet.ClojureServlet", "set-redis-pool");
		initRedis.invoke(jps);
		
		Var initdb = RT.var("com.m3958.firstgwt.server.servlet.ClojureServlet", "start");
		Properties p = pr.getPropertyMap("freetable");
		initdb.invoke(p.getProperty("user"),p.getProperty("password"));
	}
	
	private void reloadClojure(){
		if(needReload){
			try {
				RT.loadResourceScript("com/m3958/firstgwt/server/servlet/ClojureServlet.clj");
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	
	private Set<String> getFieldSet(TableSchema ts){
		String tsj = ts.getSchemaJson();
		JSONArray ja = JSONArray.fromObject(tsj);
		Set<String> set = new HashSet<String>();
		for(int i=0;i<ja.size();i++){
			set.add(ja.getJSONObject(i).getString("dbfName"));
		}
		return set;
	}
	
	public String getAddResult(HttpServletRequest req,TableSchema ts){
		reloadClojure();
		Var add = RT.var("com.m3958.firstgwt.server.servlet.ClojureServlet", "insert-one-record");
		Object o = add.invoke(req,getFieldSet(ts));
		return (String)o; 
	}

	
//	public Map getAddResult(HttpServletRequest req,TableSchema ts){
//		reloadClojure();
//		Var add = RT.var("com.m3958.firstgwt.server.servlet.ClojureServlet", "insert-one-record");
//		String tsj = ts.getSchemaJson();
//		JSONArray ja = JSONArray.fromObject(tsj);
//		Set<String> set = new HashSet<String>();
//		for(int i=0;i<ja.size();i++){
//			set.add(ja.getJSONObject(i).getString("dbfName"));
//		}
//		Object o = add.invoke(req,set);
//		return (Map)o; 
//	}
	
//	public List getFetchResult(HttpServletRequest req,TableSchema ts){
//		reloadClojure();
//		Var add = RT.var("com.m3958.firstgwt.server.servlet.ClojureServlet", "read-record-range");
//		String tsj = ts.getSchemaJson();
//		JSONArray ja = JSONArray.fromObject(tsj);
//		Set<String> set = new HashSet<String>();
//		for(int i=0;i<ja.size();i++){
//			set.add(ja.getJSONObject(i).getString("dbfName"));
//		}
//		Object o = add.invoke(req,set);
//		return (List)o; 
//	}
	public String getFetchResult(HttpServletRequest req,TableSchema ts){
		reloadClojure();
		Var add = RT.var("com.m3958.firstgwt.server.servlet.ClojureServlet", "read-record-range");
		Object o = add.invoke(req,getFieldSet(ts));
		return (String)o; 
	}
}
